#主函数
import link_SqlServer as lts
import spider
import time

def main():
    city_pinyin = ['nanchang','jingdezhen','pingxiang','jiujiang','xinyu','yingtan','ganzhou','yichun','shangrao','jian','fzhou']
    city_hanzi = ['南昌','景德镇','萍乡','九江','新余','鹰潭','赣州','宜春','上饶','吉安','抚州']
    for city_num in range(12):
        for x in range (1,12):
            if x == 1:
                url = f'https://{city_pinyin[city_num]}.jianzhimao.com'
            else:
                url = f'https://{city_pinyin[city_num]}.jianzhimao.com/index{x}.html'
            job,area,num,timer=spider.spider(url)
            time.sleep(0.3)
            for i in range(0, len(job)):
                try:
                    lts.insert_to_server(f'{city_hanzi[city_num]}',f'{job[i]}',f'{area[i]}',f'{num[i]}',f'{timer[i]}')
                except:
                    print(f"{city_hanzi[city_num]}:{job[i]}插入失败")
                continue

    print(f"{city_hanzi[city_num]}数据插入成功")


if __name__ == '__main__':
    main()